<?php

include_once "xAP-functions/functions.php";
include_once "xAP-config/config.php";


//collect Folders
$i=0;
$xAPFunctions = opendir($config["basefolder"]."/engine/");
while($entryName = readdir($xAPFunctions)) {
	if (substr($entryName,0,4)!="xAP-" && $entryName!="Default" && $entryName[0]!="."){
		if (is_dir($config["basefolder"]."/engine/".$entryName)){
			$app[$i]=$entryName;
			$i++;
		}
	}
}

$app=array();
$app[]="TV";
$app[]="Floorplan";

//fire up threads
foreach($app as $key=>$value){
	echo "Starting ".$value."\r\n";
	$t[$key] = PHPThread::create($config["basefolder"]."/engine/engine.php ".$value);
	//sleep(30); //wait for item to start
}

//loop forever
while (1){
	//echo "SRT ".time()."\r\n";
	foreach($app as $key=>$value){
	//	echo $key." Listen\r\n";
		//echo $t[$key]->listen();
	}
	/*
	if (time()>$check){
		foreach($app as $key=>$value){
			if(!$t[$key]->isActive()){
				echo $value." Stopped! - re-running\r\n";
				$t[$key]->close();
				$t[$key] = PHPThread::create($config["basefolder"]."/engine/engine.php ".$value);
			}
		}
		$check=time()+10;
	}
	*/
	//echo "END ".time()."\r\n";
}
/*
foreach($app as $key=>$value){
	echo "Starting ".$value."\r\n";
	$t[$key] = PHPThread::create($config["basefolder"]."/engine/engine.php ".$value);
}
*/


?>